
package com.google.code.netbuilder.services.equipment;

import com.google.code.netbuilder.dal.entities.Equipment;
import com.google.code.netbuilder.dal.entities.ManagedObject;
import com.google.code.netbuilder.dal.providers.IEquipmentProvider;
import com.google.code.netbuilder.dal.services.ConnectionService;
import com.google.code.netbuilder.logging.NetBuilderLogger;
import com.google.code.netbuilder.logging.NetBuilderLoggerFactory;
import com.google.code.netbuilder.util.ManagedObjectDataModel;
import java.util.LinkedList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Alexander Schrebakov
 */

@ManagedBean(name = "equipmentFindingService")
@ViewScoped
public class EquipmentFindingService {
    
    private static final NetBuilderLogger LOGGER = NetBuilderLoggerFactory.getNetBuilderLogger(ConnectionService.class);
    
    @EJB
    private IEquipmentProvider eqProvider;
    
    private List<Equipment> equipments;
    
    private ManagedObjectDataModel<Equipment> equipmentsDM;
    
    private Equipment leftSelectedEquipment;
    
    private Equipment rightSelectedEquipment;
    
    private LinkedList<ManagedObject> listEntities;
    
    @EJB
    private ConnectionService cs;
    

    public Equipment getLeftSelectedEquipment() {
        return leftSelectedEquipment;
    }

    public void setLeftSelectedEquipment(Equipment leftSelectedEquipment) {
        this.leftSelectedEquipment = leftSelectedEquipment;
    }

    public Equipment getRightSelectedEquipment() {
        return rightSelectedEquipment;
    }

    public void setRightSelectedEquipment(Equipment rightSelectedEquipment) {
        this.rightSelectedEquipment = rightSelectedEquipment;
    }
    
    public List<Equipment> getEquipments() {
        return equipments;
    }
    
    public void setEquipments(List<Equipment> equipments) {
        this.equipments = equipments;
    }

    public ManagedObjectDataModel<Equipment> getEquipmentsDM() {
        return equipmentsDM;
    }

    public void setEquipmentsDM(ManagedObjectDataModel<Equipment> equipmentsDM) {
        this.equipmentsDM = equipmentsDM;
    }

    public LinkedList<ManagedObject> getListEntities() {
        return listEntities;
    }

    public void setListEntities(LinkedList<ManagedObject> listEntities) {
        this.listEntities = listEntities;
    }
    
    
    @PostConstruct
	public void afterInitialize() {
       
        equipments = eqProvider.findAll();
        
        if(equipments != null && !equipments.isEmpty()) {
            leftSelectedEquipment = equipments.get(0);
            rightSelectedEquipment = equipments.get(0);
        }
        equipmentsDM = new ManagedObjectDataModel<Equipment>(equipments);
        
	}
    
    public void find() {
        
        FacesMessage message = null;
        
        if(leftSelectedEquipment == null || rightSelectedEquipment == null) {
           message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "At least one of equipments not selected", null);
           FacesContext.getCurrentInstance().addMessage(null, message);
           return;
        }
        
        try {
            listEntities = cs.findPath(leftSelectedEquipment, rightSelectedEquipment);
            if (listEntities.isEmpty()) {
                message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Path not found.", null);
            }
            else {
                message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Path found successfully.", null);
            }            
        }
        catch (Exception ex) {
            message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "The same equipment.", null);
        }
        finally {
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
        
    }
    
}
